// HTTP 客户端使用示例
import { http } from './http'
import type { ApiResponse } from './http'

// 示例：用户数据接口
interface User {
  id: number
  name: string
  email: string
}

// 示例：文章数据接口
interface Article {
  id: number
  title: string
  content: string
  author: string
  createdAt: string
}

// API 调用示例
export class ApiService {
  // GET 请求示例
  static async getUsers(): Promise<ApiResponse<User[]>> {
    return http.get('/users')
  }

  // GET 请求带参数示例
  static async getUserById(id: number): Promise<ApiResponse<User>> {
    return http.get(`/users/${id}`)
  }

  // POST 请求示例
  static async createUser(userData: Omit<User, 'id'>): Promise<ApiResponse<User>> {
    return http.post('/users', userData)
  }

  // PUT 请求示例
  static async updateUser(id: number, userData: Partial<User>): Promise<ApiResponse<User>> {
    return http.put(`/users/${id}`, userData)
  }

  // DELETE 请求示例
  static async deleteUser(id: number): Promise<ApiResponse<void>> {
    return http.delete(`/users/${id}`)
  }

  // 带查询参数的 GET 请求示例
  static async getArticles(params: {
    page?: number
    limit?: number
    category?: string
  } = {}): Promise<ApiResponse<Article[]>> {
    return http.get('/articles', { params })
  }
}

// 使用示例
export async function exampleUsage() {
  try {
    // 获取用户列表
    const usersResponse = await ApiService.getUsers()
    console.log('Users:', usersResponse.data)

    // 获取特定用户
    const userResponse = await ApiService.getUserById(1)
    console.log('User:', userResponse.data)

    // 创建新用户
    const newUser = await ApiService.createUser({
      name: 'John Doe',
      email: 'john@example.com'
    })
    console.log('New User:', newUser.data)

    // 获取文章列表（带分页）
    const articlesResponse = await ApiService.getArticles({
      page: 1,
      limit: 10,
      category: 'tech'
    })
    console.log('Articles:', articlesResponse.data)

  } catch (error) {
    console.error('API Error:', error)
  }
}
